#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Solution {
public:
    bool check(int* phash, int* shash, int n)
    {
        for (int i = 0; i < 27; i++)
        {
            if (phash[i] != shash[i])
                return false;
        }

        return true;
    }

    vector<int> findAnagrams(string s, string p) {
        int phash[27] = { 0 };
        int shash[27] = { 0 };

        int plen = p.size();
        for (int i = 0; i < plen; i++)
        {
            phash[p[i] - 'a']++;
        }

        vector<int> ans;
        int n = s.size(), slen = 0;
        for (int left = 0, right = 0; right < n; right++)
        {
            if (slen < plen)
            {
                shash[s[right] - 'a']++;
                slen++;
            }

            if (slen == plen && check(phash, shash, 27))
            {
                ans.push_back(left);
            }

            if (slen == plen)
            {
                shash[s[left++] - 'a']--;
                slen--;
            }
        }

        return ans;
    }
};